﻿using MaterialDesignThemes.Wpf;
using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyFirstWPF.View
{
    /// <summary>
    /// Page4.xaml 的交互逻辑
    /// </summary>
    public partial class Page4 : Page
    {
        public static string dbPath = "../../studyPlan.db";
        private const String DEFAULT_TEXT = "标题";

        public Page4()
        {
            InitializeComponent();
            reInitDiarys();
            //默认文本方法
            SetDefaultText();

            //焦点事件
            //得到焦点时
            title.GotFocus += new RoutedEventHandler(Text_Enter);
            //失去焦点时
            title.LostFocus += new RoutedEventHandler(Text_Leave);
        }

        private void SetDefaultText()
        {
            //默认文本
            title.Text = DEFAULT_TEXT;
            //默认文本颜色为灰色
            title.Foreground = new SolidColorBrush(Colors.Gray);
        }

        //获取焦点事件 Enter
        private void Text_Enter(object sender, EventArgs e)
        {
            if (title.Text == DEFAULT_TEXT)
            {
                //清空文本框
                title.Text = "";
                title.Foreground = new SolidColorBrush(Colors.Black);
            }

        }

        //失去焦点事件 Leave
        private void Text_Leave(object sender, EventArgs e)
        {
            //如果文本框为空，则显示默认文本
            if (String.IsNullOrEmpty(title.Text))
            {
                //默认文本
                SetDefaultText();
            }
        }

        //关闭界面
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            clearDialogData();
            DialogHost.CloseDialogCommand.Execute(null, null);
        }

        private void clearDialogData()
        {
            // 清空输入数据
            title.Text = "";
            InputDiary.Text = "";
        }

        //保存
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            //判断是否有标题
            if (title.Text == DEFAULT_TEXT)
            {
                title.Text = "无标题";
            }
            //判断日志内容是否为空
            if (InputDiary.Text == "")
            {
                StackPanel st = new StackPanel();
                st.Margin = new Thickness(16);
                TextBlock tb = new TextBlock();
                tb.FontSize = 20;
                tb.Text = "日志内容不能为空";
                st.Children.Add(tb);
                Button bt = new Button();
                bt.Content = "ok";
                bt.Click += (oo, ee) =>
                {
                    DialogHost.CloseDialogCommand.Execute(null, null);
                    clearDialogData();
                };
                st.Children.Add(bt);
                DialogHost.OpenDialogCommand.Execute(st, null);
                return;
            }

            //将新建的不为空的日志写入数据库
            using (var db = new StudyPlanDb(dbPath))
            {
                DiaryInfo diary = new DiaryInfo();
                diary.DiaryTitle = title.Text;
                diary.Diary = InputDiary.Text;
                diary.BuildTime = System.DateTime.Now.ToString("f");
                diary.userId = LoginView.myId;
                db.Insert(diary);
                reInitDiarys();
            }
            clearDialogData();
            DialogHost.CloseDialogCommand.Execute(null, null);
        }

        //将数据库中的数据取出
        public void reInitDiarys()
        {
            wp4.Children.Clear();
            using (var db = new StudyPlanDb(dbPath))
            {
                List<DiaryInfo> diaryList = db.Diarys.Where(x => x.userId == LoginView.myId).ToList();
                diaryList.Reverse();
                for (int i = 0; i < diaryList.Count; i++)
                {
                    if (diaryList[i].TaskId == 0)
                    {
                        wp4.Children.Add(new UserControl3(diaryList[i]));
                    }
                }
            }
        }


    }
}
